﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;

namespace SmartUI.Assist
{
    public class DataGridAssist
    {
        public static string GetCollumsScale(DataGrid dataGrid) => (string)dataGrid.GetValue(CollumsScaleProperty);
        public static void SetCollumsScale(DataGrid dataGrid, string value) => dataGrid.SetValue(CollumsScaleProperty, value);

        public static readonly DependencyProperty CollumsScaleProperty
           = DependencyProperty.RegisterAttached("CollumsScale", typeof(string), typeof(DataGridAssist),
               new PropertyMetadata(CollumsScaleChanged));

        private static void CollumsScaleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is null || !(d is DataGrid dataGrid))
                return;
            dataGrid.AutoGeneratedColumns += DataGrid_AutoGeneratedColumns;
            dataGrid.Loaded += DataGrid_Loaded;
            dataGrid.SizeChanged += DataGrid_SizeChanged;
        }

        private static void DataGrid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            DataGrid_AutoGeneratedColumns(sender, null);
        }

        private static void DataGrid_Loaded(object sender, RoutedEventArgs e)
        {
            DataGrid_AutoGeneratedColumns(sender, null);
        }

        private static void DataGrid_AutoGeneratedColumns(object sender, EventArgs e)
        {
            if (!(sender is DataGrid dataGrid) || GetCollumsScale(dataGrid) == default)
                return;
            string str = GetCollumsScale(dataGrid);
            if (string.IsNullOrEmpty(str))
                return;
            int columsCount = dataGrid.Columns.Count;
            string[] splits = new string[columsCount];
            if (str.Equals("*"))
            {
                splits = new string[dataGrid.Columns.Count];
                for (int i = 0; i < splits.Length; i++)
                {
                    splits[i] = "*";
                }
            }
            else if (str.Split(' ').Length == columsCount)
            {
                splits = str.Split(' ');
            }
            else if (str.Split(',').Length == columsCount)
            {
                splits = str.Split(',');
            }
            ScaleColumsWidth(dataGrid, splits);
        }

        private static void ScaleColumsWidth(DataGrid dataGrid, string[] scales)
        {
            if (scales is null || scales.Length == 0 || dataGrid.Columns.Count != scales.Length)
                return;
            double[] widths = new double[scales.Length];
            double[] widthScales = new double[scales.Length];
            for (int i = 0; i < scales.Length; i++)
            {
                if (double.TryParse(scales[i], out double number))
                {
                    widths[i] = number;
                }
                else if (scales[i].EndsWith("*"))
                {
                    string temp = scales[i].Substring(0, scales[i].Length - 1);
                    temp = string.IsNullOrWhiteSpace(temp) ? "1" : temp;
                    if (double.TryParse(temp, out double scale))
                    {
                        widthScales[i] = scale;
                    }
                }
                else
                {
                    widthScales[i] = 1;
                }
            }
            if (widthScales.Sum() > 0)
            {
                double segmentWidth = (dataGrid.ActualWidth - widths.Sum()) / widthScales.Sum();
                for (int i = 0; i < scales.Length; i++)
                {
                    if (widthScales[i] == 0)
                        continue;
                    widths[i] = widthScales[i] * segmentWidth;
                }
            }
            for (int i = 0; i < dataGrid.Columns.Count; i++)
            {
                dataGrid.Columns[i].Width = widths[i];
            }
        }
    }
}
